package com.studentstreet.tools.dbhelper;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.studentstreet.entities.SellerEntity;

public class SellerDBHelper {
	private DBHelper dbHelper = null;

	public SellerDBHelper(Context context) {
		super();
		dbHelper = new DBHelper(context);
	}

	public long saveToDB(String json) {
		List<SellerEntity> sellerList = new ArrayList<SellerEntity>();
		Gson gson = new Gson();
		Type listType = new TypeToken<ArrayList<SellerEntity>>() {
		}.getType();
		sellerList = gson.fromJson(json, listType);
		return saveToDB(sellerList);
	}

	public long saveToDB(List<SellerEntity> sellerList) {
		long row = 0L;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "delete from " + DBTools.TABLE_SELLER;
		db.execSQL(sql);
		for (SellerEntity seller : sellerList) {
			ContentValues values = new ContentValues();
			values.put(DBTools.SELLER_ID, seller.getId());
			values.put(DBTools.SELLER_NAME, seller.getName());
			values.put(DBTools.SELLER_TEL, seller.getTel());
			values.put(DBTools.SELLER_ADDR, seller.getAddress());
			values.put(DBTools.SELLER_LOGO, seller.getLogoURL());
			values.put(DBTools.SELLER_OWNER, seller.getOwner());
			row += db.insert(DBTools.TABLE_SELLER, null, values);
		}
		db.close();
		return row;
	}
}
